package me.lc.hammerheads.sprite.dwarfUnit;

import me.lc.hammerheads.core.GameManager;
import me.lc.hammerheads.core.GameSystem;
import me.lc.hammerheads.sprite.Dwarf;
import me.lc.hammerheads.util.AudioHelper;

import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.Animate;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.nodes.Animation;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.nodes.Animation.IAnimationCallback;
import com.wiyun.engine.types.WYRect;

/**
 * 铁通帽子男人
 * 
 * @author Administrator
 * 
 */
public class DwarfHatMan extends Dwarf implements IAnimationCallback {

	private static float ItemWidth = GameSystem.tex_hatMan.getWidth() / 12.0f;
	private static float ItemHeight = GameSystem.tex_hatMan.getHeight() / 3.0f;
	private int leaveLife = getLifeCount();

	// 是否已经戴上铁桶
	private boolean isHated;

	public boolean isHated() {
		synchronized (isHatedSyncObj) {
			return isHated;
		}
	}

	private Object isHatedSyncObj = new Object();

	@Override
	protected void createDwart() {
		dwarf = Sprite.make(GameSystem.tex_hatMan, getFrame(0, 0));
	}

	@Override
	public int knock(int power) {
		synchronized (isHatedSyncObj) {
			if (isHated) {
				GameManager.instance.changeGameHeart(false);
				return 2;
			} else {
				leaveLife -= power;
				if (leaveLife <= 0) {
					dwarf.stopAllActions();
					knockOut();

				} else {
					AudioHelper.manAU();
					blood.update(leaveLife);
				}
				return 1;
			}
		}
	}

	@Override
	protected int getLifeTime() {
		return 8;
	}

	@Override
	protected int getLifeCount() {
		return 2;
	}

	@Override
	protected float getDwarfX() {
		return ItemWidth * 0.1530612244897959f;
	}

	private WYRect getFrame(int row, int frame) {
		return WYRect.make(frame * ItemWidth, row * ItemHeight, ItemWidth,
				ItemHeight - 1);
	}

	@Override
	protected void doDwarfAction() {
		// 开始动画,戴铁桶动画
		Animation animation = new Animation(0);
		animation.addFrame(0.1f, getFrame(2, 11), getFrame(2, 11),
				getFrame(2, 11), getFrame(1, 0), getFrame(1, 1),
				getFrame(1, 2), getFrame(1, 3), getFrame(1, 4), getFrame(1, 5),
				getFrame(1, 6), getFrame(1, 7), getFrame(1, 8), getFrame(1, 9),
				getFrame(1, 10), getFrame(2, 0), getFrame(2, 1),
				getFrame(2, 2), getFrame(2, 3), getFrame(2, 4), getFrame(2, 5),
				getFrame(2, 6), getFrame(2, 7), getFrame(2, 8), getFrame(2, 9),
				getFrame(2, 10), getFrame(2, 11), getFrame(2, 11),
				getFrame(2, 11), getFrame(2, 11), getFrame(2, 11),
				getFrame(2, 11), getFrame(2, 11));
		animation.setCallback(this);
		Animate a = (Animate) Animate.make(animation).autoRelease();
		Action repeat = (Action) RepeatForever.make(a).autoRelease();
		dwarf.runAction(repeat);
	}

	@Override
	protected float getDwarfWidth() {
		return ItemWidth;
	}

	@Override
	public void onAnimationEnded(int arg0) {

	}

	@Override
	public void onAnimationFrameChanged(int arg0, int arg1) {
		// 12-18帧为戴上铁桶的时间
		synchronized (isHatedSyncObj) {
			if (arg1 >= 10 && arg1 <= 16) {
				isHated = true;
			} else {
				isHated = false;
			}
		}
	}

	@Override
	protected int getKnockoutScore() {
		return 300;
	}
}
